var filePath = "~/Documents/SketchSymbols/",
    separator = "+";

function exportWebSymbols(context) {
    let options = [
        { scale : '1', suffix : '.svg' },
    ];
    let api = context.api();
    api.alert("No Selected Symbols");
    exportSymbols(options,context);
}

function exportSymbols(options,context) {


    let api = context.api();
    let selectedLayers = context.selection;
    let selectedCount = selectedLayers.count();



    if (selectedCount == 0) {

        return;
    }

    for (var i = 0,len = selectedCount; i<len; i++) {

        var layer = selectedLayers[i],
            symbolName = setName(layer);

        options.forEach(function(option) {
            context.document.saveExportRequest_toFile(getSlice(layer,option.scale),filePath+symbolName+option.suffix);
        });

        api.message("had Exported successfully to "+filePath);
    }

};

function setIconName(context) {


    let selectedLayers = context.selection;
    let selectedCount = selectedLayers.count();

    if (selectedCount == 0) {
        Dialog.newInformationDialog("提示","请先选中需要命名的图层");
        return;
    }

    let selectedLayer = selectedLayers[0];
    let userInput = Dialog.newIconNameDialog(selectedLayers[0]);

    switch (userInput.responseCode) {

        case 1000: //用户点击确认

            let suffix = "+";

            if(userInput.name) {
                let tmpArray = userInput.name.split("");
                tmpArray[0] = tmpArray[0].toUpperCase();
                suffix += tmpArray.join("");
            }

            if(userInput.size!="大小"){
                suffix += userInput.size;
            }
            if(userInput.color!="颜色"){
                suffix += userInput.color;
            }
            if(userInput.fill!="是否镂空"){
                suffix += userInput.fill;
            }
            selectedLayer.setName(selectedLayer.name()+suffix);
            break;

        default: //Just close the dialog
            break;
    };

}


function setName(layer) {
    var symbolFullName = layer.name();
    return symbolFullName.slice(symbolFullName.indexOf(separator)+1);
}


function getSlice (layer,scale) {

    var exportOptions,
        exportSize,
        symbolSlice;

    exportOptions = layer.exportOptions();
    exportOptions.removeAllExportFormats();

    exportSize = exportOptions.addExportFormat();
    exportSize.setScale(scale);

    symbolSlice =  MSExportRequest.exportRequestsFromExportableLayer_exportFormats_useIDForName(layer,exportOptions.exportFormats(),false).firstObject();

    exportOptions.removeExportFormat(exportSize);

    return symbolSlice;


}


